import java.io.*;
import java.net.Socket;

/**
 * Created by lenovo on 2018/11/7.
 */
public class BIOSockerClient {
    public static void main(String [] args){

        System.out.println("客户端启动...");
        System.out.println("当接收到服务器端字符为 \"OK\" 的时候, 客户端将终止\n");

        while (true) {

            Socket socket = null;
            try {

                socket = new Socket("127.0.0.1", 8080);
                DataInputStream inputStream = new DataInputStream(socket.getInputStream());

                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

                System.out.print("请输入:");
                String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
                outputStream.writeUTF(str);

                String readStr = inputStream.readUTF();
                System.out.println("服务器端返回过来的是: " + readStr);

                outputStream.close();
                inputStream.close();

                if (readStr.equals("ok")) {
                    break;
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        socket = null;
                    }
                }
            }

        }
    }
}
